package com.yesep.common.domain;



import com.alibaba.fastjson.JSONObject;
import com.yesep.common.constant.RetEnum;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: envm-core
 * @description: 响应类
 * @author: quliang
 * @create: 2019-07-12 09:56
 **/
@Data
public class EnvmResponse implements Serializable {

    private static final long serialVersionUID = -7689597618599589997L;

    private final Logger log = LoggerFactory.getLogger(getClass());
    /**
     * 响应码
     */
    private int code;
    /**
     * 响应消息
     */
    private String msg;
    /**
     * 响应数据
     */
    private Object data;

    public EnvmResponse(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        log.info(this.toString());
    }

    public EnvmResponse(RetEnum retEnum, Object data) {
        this.code = retEnum.getCode();
        this.msg = retEnum.getMessage();
        this.data = data;
        log.info(this.toString());
    }

    public static EnvmResponse build(RetEnum retEnum) {
        EnvmResponse envmResponse = new EnvmResponse(retEnum.getCode(), retEnum.getMessage(), null);
        return envmResponse;
    }

    public static EnvmResponse build(RetEnum retEnum, Object data) {
        EnvmResponse envmResponse = new EnvmResponse(retEnum.getCode(), retEnum.getMessage(), data);
        return envmResponse;
    }

    public static EnvmResponse buildSuccess() {
        return buildSuccess(null);
    }

    private static EnvmResponse buildSuccess(Object data) {
        EnvmResponse envmResponse = new EnvmResponse(RetEnum.RET_COMM_SUCCESS, data);
        return envmResponse;
    }

    private static EnvmResponse buildSuccess(Object data, JSONObject param) {
        if (param != null && param.getBooleanValue("returnArray")) {
            List<Object> objects = new LinkedList<Object>();
            objects.add(data);
            return new EnvmResponse(RetEnum.RET_COMM_SUCCESS, objects);
        } else {
            return new EnvmResponse(RetEnum.RET_COMM_SUCCESS, data);
        }

    }
}
